﻿using System;
using System.Configuration;
using System.Runtime.Caching;

namespace Ruandao.Account.EntityAccess
{
    using Ruandao.MiniAccess;
    using Ruandao.MiniAccess.SqlServer;
    using Ruandao.Foundation;
    using Ruandao.HostServer;

    public sealed partial class AccountAccess
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static AccountAccess GetInstance()
        {
            ObjectCache cache = MemoryCache.Default;
            string connString = GetConnectionString();
            string instanceChaeKey = "ACCOUNTACCESS_INSTANCES_" + connString;
            AccountAccess instance = cache[instanceChaeKey] as AccountAccess;

            if (instance == null)
            {
                instance = new AccountAccess(connString);
                CacheItemPolicy policy = new CacheItemPolicy();
                cache.Set(instanceChaeKey, instance, policy);
            }
            return instance;
        }

        private static string GetConnectionString()
        {
            string connectionString = "";
            string dbname = ConfigurationManager.AppSettings["AccountConnectionName"];
            if (string.IsNullOrEmpty(dbname))
            {
                dbname = "AccountDatabase";
            }
            connectionString = ConfigurationManager.ConnectionStrings[dbname].ConnectionString;
            if (string.IsNullOrEmpty(connectionString))
            {
                connectionString = SiteWare.Current.Site.ConnectionString;
            }
            return connectionString;
        }


    }
}
